﻿-- =============================================
-- Author:		<GSJ>
-- Create date: <2013-09-4>
-- Description:	<获得符合条件的能申领的客户数量>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Customer_AllowExtractBD_Count] 
	(
		@UserName varchar(20),
		@IndustryOneId varchar(20),
		@IndustryTwoId varchar(20)
	)
AS	
Begin
	Declare @companyid int,@num int,@Days int
	Set @Days = -10		--10日内批注过的客户不再参与申领
	EXEC @companyid=proc_Company_GetID @UserName,@companyid--单位ID
	If @IndustryOneId='0'	--所有行业
			Select Count(0) As shl From Customer Where Companyid=@companyid and Audit=1 and 
				(ProtectName is null) and ProtectId=3 And UpTime<DateAdd(day,@Days,GetDate()) And
				(CustomerName Not like '%免打扰%') And delFlag=0
				and  customerid in ( select distinct cusid from [Project] where [ProductId]=1 and Companyid=@companyid)
 
	Else	--指定的一级行业
		If @IndustryTwoId='0'	--所有指定的一级行业
			Select Count(0) As shl From Customer Where Companyid=@companyid and Audit=1 and 
				(ProtectName is null) and ProtectId=3 And UpTime<DateAdd(day,@Days,GetDate()) And
				(CustomerName Not like '%免打扰%') And IndustryOneId=Convert(int,@IndustryOneId) And delFlag=0
				and  customerid in (  select distinct cusid from [Project] where [ProductId]=1 and Companyid=@companyid)
 
		Else	--指定的二级行业
			Select Count(0) As shl From Customer Where Companyid=@companyid and Audit=1 and 
				(ProtectName is null) and ProtectId=3 And UpTime<DateAdd(day,@Days,GetDate()) And
				(CustomerName Not like '%免打扰%') And IndustryTwoId=Convert(int,@IndustryTwoId) And delFlag=0
				and  customerid in (  select distinct cusid from [Project] where [ProductId]=1 and Companyid=@companyid)
End